home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / suck-2.6 / suck-2 / suck-2.6.3 / timer.c < prev    next >
C/C++ Source or Header  |  1995-11-26  |  2KB  |  71 lines

  1. #include <stdio.h>
  2. #include <sys/time.h>
  3. #include <unistd.h>
  4. #include "timer.h"
  5.  
  6. /*internal functions */
  7. double get_elapsed(struct timeval *);
  8.  
  9. /*-----------------------------------------------------------*/
  10. char *TimerFunc(int which_function, long nradd) { 
  11.  
  12.     static struct timeval start;
  13.     static long nrbytes;
  14.     static char retdisp[256];
  15.     static char none[] = { "" };    
  16.  
  17.     double elapsed, bps;
  18.     long mins;        /* long so can get mins to exact decimal */
  19.     char *retval = none;
  20.  
  21.     switch(which_function) {
  22.       case TIMER_START:
  23.         nrbytes = 0L;
  24.         gettimeofday(&start, NULL);
  25.         break;
  26.       case TIMER_ADDBYTES:
  27.         nrbytes += nradd;
  28.         break;
  29.       case TIMER_DISPLAY:
  30.         if(nrbytes > 0) {
  31.             elapsed = get_elapsed(&start);
  32.             bps = (elapsed > 0.0) ? bps = nrbytes / elapsed : 0.0;
  33.             sprintf(retdisp,"%10.1f BPS", bps);
  34.             retval = retdisp;
  35.         }
  36.         break;
  37.       case TIMER_TIMEONLY:
  38.         elapsed = get_elapsed(&start);
  39.         mins = ((long) elapsed) / 60 ;        /* get minutes */
  40.         elapsed -= (mins * 60);        /* subtract to get remainder */
  41.         sprintf(retdisp, "Elapsed Time = %ld mins %.2f seconds", mins, elapsed);
  42.         retval = retdisp;
  43.         break;
  44.       case TIMER_TOTALS:
  45.         elapsed = get_elapsed(&start);
  46.         bps = (elapsed > 0.0 && nrbytes > 0) ? bps = nrbytes / elapsed : 0.0;
  47.  
  48.         mins = ((long) elapsed) / 60 ;        /* get minutes */
  49.         elapsed -= (mins * 60);        /* subtract to get remainder */
  50.         sprintf(retdisp, "%ld Bytes Received in %ld mins %.2f seconds, BPS = %-10.1f", nrbytes, mins, elapsed, bps);
  51.         retval = retdisp;
  52.         break;    
  53.       default:
  54.         /* ignore invalid commands */
  55.         break;
  56.     }
  57.     return retval;
  58. }
  59. /*-----------------------------------------------------------------------------------*/
  60. double get_elapsed(struct timeval *start) {
  61.     struct timeval curr;
  62.     double elapsed;
  63.  
  64.     /* compute elapsed time, in seconds */
  65.     gettimeofday(&curr, NULL);
  66.     elapsed = curr.tv_sec - start->tv_sec;
  67.     elapsed += (((double) (curr.tv_usec - start->tv_usec)) / 1000000.0);
  68.  
  69.     return elapsed;
  70. }
  71.